package com.sz.mes.controller.warehouse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sz.mes.mapper.warehouse.ShelvesMapper;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.diboot.core.vo.*;
import com.diboot.iam.annotation.OperationCons;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.sz.mes.entity.warehouse.Shelves;
import com.sz.mes.dto.warehouse.ShelvesDTO;
import com.sz.mes.vo.warehouse.ShelvesListVO;
import com.sz.mes.vo.warehouse.ShelvesDetailVO;
import com.sz.mes.service.warehouse.ShelvesService;
import com.sz.mes.controller.BaseCustomCrudRestController;

import lombok.extern.slf4j.Slf4j;
import javax.validation.Valid;
import java.util.List;

/**
 * 货区货架 相关Controller
 *
 * @author hem
 * @version 1.0
 * @date 2023-05-25
 * Copyright © szhs
 */
@RestController
@RequestMapping("/warehouse/shelves")
@BindPermission(name = "货区货架", code="WarehouseShelves")
@Slf4j
public class ShelvesController extends BaseCustomCrudRestController<Shelves> {
    @Autowired
    private ShelvesService shelvesService;

    @Autowired
    private ShelvesMapper shelvesMapper;

    /**
     * 查询ViewObject的分页数据
     * <p>
     * url请求参数示例: /list?field=abc&pageIndex=1&orderBy=abc:DESC
     * </p>
     *
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_LIST)
//    @BindPermission(name = OperationCons.LABEL_LIST, code = OperationCons.CODE_READ)
    @GetMapping("/list")
    public JsonResult getViewObjectListMapping(ShelvesDTO queryDto, Pagination pagination) throws Exception{
    		return super.getViewObjectList(queryDto, pagination, ShelvesListVO.class);
    }

    /**
     * 根据资源id查询ViewObject
     *
     * @param id ID
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DETAIL)
//    @BindPermission(name = OperationCons.LABEL_DETAIL, code = OperationCons.CODE_READ)
    @GetMapping("/{id}")
    public JsonResult getViewObjectMapping(@PathVariable("id")Long id) throws Exception{
        return super.getViewObject(id, ShelvesDetailVO.class);
    }

    /**
     * 创建资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_CREATE)
//    @BindPermission(name = OperationCons.LABEL_CREATE, code = OperationCons.CODE_WRITE)
    @PostMapping("/")
    public JsonResult createEntityMapping(@Valid @RequestBody ShelvesDTO entity) throws Exception {
        try{
            //根据货架编号和货区id确定货架唯一
            Long warehouseAreaId = entity.getWarehouseAreaId();
             String slelfNumber = entity.getSlelfNumber();
            QueryWrapper<Shelves> shelvesQueryWrapper = new QueryWrapper<>();
            shelvesQueryWrapper.eq("warehouse_area_id",warehouseAreaId);
            shelvesQueryWrapper.eq("slelf_number",slelfNumber);
            Shelves shelves = shelvesMapper.selectOne(shelvesQueryWrapper);
            if (shelves==null){
                super.createEntity(entity);
                shelvesService.creatLattice(entity);
                return JsonResult.OK("操作成功");
            }else {
                return JsonResult.FAIL_OPERATION("货架编号重复");
            }
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.FAIL_OPERATION("货架编号重复");
        }
    }

    /**
     * 根据ID更新资源对象
     *
     * @param entity
     * @return JsonResult
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_UPDATE)
//    @BindPermission(name = OperationCons.LABEL_UPDATE, code = OperationCons.CODE_WRITE)
    @PutMapping("/{id}")
    public JsonResult updateEntityMapping(@PathVariable("id")Long id, @Valid @RequestBody Shelves entity) throws Exception {
        return super.updateEntity(id, entity);
    }

    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    /*@Log(operation = OperationCons.LABEL_DELETE)
//    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult deleteEntityMapping(@PathVariable("id")Long id) throws Exception {
        return super.deleteEntity(id);
    }*/



    /**
     * 根据id删除资源对象
     *
     * @param id
     * @return
     * @throws Exception
     */
    @Log(operation = OperationCons.LABEL_DELETE)
//    @BindPermission(name = OperationCons.LABEL_DELETE, code = OperationCons.CODE_WRITE)
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id")Long id) throws Exception {
        return shelvesService.delete(id);
    }


}
